

*------------------------------------------------------------------------------*
* Merge with price data *
*------------------------------------------------------------------------------*

*----- Merge
use "data/working/data_platform.dta", clear
merge m:1 currency_id date using "data/working/coinmarketcap"
drop if _merge == 2
drop _merge


*----- logME
gen logME_new = log(marketcap)


merge m:1 date currency_id using "data/working/logME" // this one I am not sure where the data comes from
drop if _merge == 2
drop _merge

corr logME_new logME // =1 

*gen logME = log(marketcap)

sort household_id date currency_id

*------------------------------------------------------------------------------*
* Merge with log_sp500 log_libor *
*------------------------------------------------------------------------------*
merge m:1 date using "data/working/coinmarketcap_bitcoin2.dta", keepusing(log_sp500 log_libor) // this one I am not sure where the data comes from
drop if _merge == 2
drop _merge


*------------------------------------------------------------------------------*
* Merge with IV - supply *
*------------------------------------------------------------------------------*
*---- Merge
merge m:1 date currency_id using "data/working/cryptocurrency",keepusing(supply)
drop if _merge == 2
drop year
gen year = year(date)
rename close price


*---- Generate indicator for one obs in each currency -date
bys currency_id date: gen no_tem = _n
gen firstobs = no_tem == 1
replace firstobs = 2 if currency_id == "ripple" & firstobs == 1
replace firstobs = 2 if currency_id == "ALT" & firstobs == 1 // this is the only novelty from Jiarui email 20231009
drop no_tem



*---- Calculate the average
* all
bys date: egen mean_all_tem = mean(supply) if firstobs == 1
bys date: egen mean_all = max(mean_all_tem)
drop mean_all_tem




*---- Assign to ALT and ripple
replace supply = mean_all if currency_id == "ALT"| currency_id == "ripple"
gen logsupply = log(supply)
encode currency_id,gen(dcurrency_id)





*------------------------------------------------------------------------------*
* Merge with beta and volatility data *
*------------------------------------------------------------------------------*
drop _merge
merge m:1 date currency_id using "data/working/beta",keepusing(beta volatility7 volatility30 momentum1 momentum2 momentum3 momentum4)
drop if _merge == 2


*---- Calculate the average
bys date: egen beta_all_tem = mean(beta) if firstobs == 1 | firstobs == 2
bys date: egen beta_all = max(beta_all_tem)
drop beta_all_tem
replace beta = beta_all if currency_id == "ALT"

foreach volatility of var volatility7 volatility30 {
   bys date: egen `volatility'_all_tem = mean(`volatility') if firstobs == 1 | firstobs == 2
   bys date: egen `volatility'_all = max(`volatility'_all_tem)
   drop `volatility'_all_tem
   replace `volatility' = `volatility'_all if currency_id == "ALT"
 }

foreach momentum of var momentum1 momentum2 momentum3 momentum4 {
   bys date: egen `momentum'_all_tem = mean(`momentum') if firstobs == 1 | firstobs == 2
   bys date: egen `momentum'_all = max(`momentum'_all_tem)
   drop `momentum'_all_tem
   replace `momentum' = `momentum'_all if currency_id == "ALT"
} 



*------------------------------------------------------------------------------*
* Save output *
*------------------------------------------------------------------------------*

save "data/working/working_platform.dta", replace






